package cn.com.dhcc.app.pub.module.logmgr.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.base.BaseDao;
import cn.com.dhcc.app.core.base.BaseService;
import cn.com.dhcc.app.core.base.CmCommon;
import cn.com.dhcc.app.core.component.db.orm.WhereCondition;
import cn.com.dhcc.app.core.component.db.orm.valuebean.BetweenValue;
import cn.com.dhcc.app.core.component.db.orm.valuebean.LikeValue;
import cn.com.dhcc.app.core.component.pager.Pager;
import cn.com.dhcc.app.core.component.pager.PagerData;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.para.ComponentPara.LOG;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.pub.core.AppIconBeanCons;
import cn.com.dhcc.app.pub.module.logmgr.vo.LogBean;
import cn.com.dhcc.app.pub.module.logmgr.vo.LogBean.LOG_TYPE;
/**
 * 日志管理
 * @author qiao
 * @2015-7-6 @上午11:59:46
 */
@Service(AppIconBeanCons.LOGMGR_SERVICE)
public class LogMgrService extends BaseService{

	/**
	 * 获取日志列表
	 * @return
	 * @throws ServiceException 
	 */
	public PagerData<LogBean> getLogList(HttpSession httpSession,LogBean logBean,Pager pager) throws ServiceException{
		String[] canses = getCanSeeLogTypes(httpSession,logBean.getLogType());
		if(canses==null||canses.length==0){
			return new PagerData<LogBean>();
		}
		WhereCondition condition = new WhereCondition();
		condition
		.where1Eq1()
		.andEq("id", logBean.getId())
		.andLike("module", LikeValue.roundLike(logBean.getModule()))
		.andLike("action", LikeValue.roundLike(logBean.getAction()))
		.andLike("userType", logBean.getUserType())
		.andLike("userId", LikeValue.roundLike(logBean.getUserId()))
		.andLike("userName", LikeValue.roundLike(logBean.getUserName()))
		.andLike("requestIp", LikeValue.roundLike(logBean.getRequestIp()))
		.andEq("log_level", logBean.getLevel())
		.andEq("result", logBean.getResult())
		.andIn("log_type", canses)
		.andLike("line", LikeValue.roundLike(logBean.getLine()))
		.andLike("msg", LikeValue.roundLike(logBean.getMsg()))
		.andBetween("log_time", new BetweenValue(logBean.getBeginDate(), logBean.getEndDate()))
		.orderBy(" log_time desc ");
		try {
			return CmCommon.getDao().getPagerModuleList("查询日志列表", LogBean.class, condition, pager);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	public static String[] getCanSeeLogTypes(HttpSession httpSession,String logTypeSelected){
		
		Set<String> cans = new HashSet<String>();
		if(BaseController.isSuperAdmin(httpSession)){
			cans.add(LOG_TYPE.SAFELOG.getCode());
			cans.add(LOG_TYPE.SYSLOG.getCode());
			cans.add(LOG_TYPE.BUSILOG.getCode());
			cans.add(LOG_TYPE.RUNLOG.getCode());
		}else{
			boolean hasAuditRole = BaseController.hasAuditRole(httpSession);
			//boolean hasBusiRole = BaseController.hasBusiRole(httpSession);
			//boolean hasSafeRole = BaseController.hasSafeRole(httpSession);
			boolean hasSysMgrRole = BaseController.hasSysMgrRole(httpSession);
			//审计员看安全员和系统管理员日志
			if(hasAuditRole){
				cans.add(LOG_TYPE.SAFELOG.getCode());
				cans.add(LOG_TYPE.SYSLOG.getCode());
			}
			//系统管理员看后台日志
			if(hasSysMgrRole){
				cans.add(LOG_TYPE.RUNLOG.getCode());
				cans.add(LOG_TYPE.BUSILOG.getCode());
			}
			//安全保密员和业务人员不能看日志
		}
		if(StrUtil.isNotBank(logTypeSelected)){
			if(!cans.contains(logTypeSelected)){
				return null;
			}else{
				return new String[]{logTypeSelected};
			}
		}
		String[] rs = new String[cans.size()];
		return cans.toArray(rs);
	}
	
	/**
	 * 批量删除日志
	 * @param args
	 * @throws ServiceException
	 */
	public void batchDeleteLogs(List<Object[]> args,boolean allAppenderSameHour)throws ServiceException{
		String sql = "delete from "+BaseDao.WSD_TABLE(LOG.LOG_TODB_TABLE)+" where module=? and log_time<?";
		if(allAppenderSameHour){
			sql = "delete from "+BaseDao.WSD_TABLE(LOG.LOG_TODB_TABLE)+" where  log_time<?";
		}
		try {
			CmCommon.getDao().batchDelete("批量删除日志", sql, args);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
}
